<template>
  <div class="loginPage">
    <el-card class="login-card">
      <div slot="header" class="clearfix">
        <span>登录</span>
      </div>
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="60px"
        class="login-form"
      >
        <el-form-item label="用户名" prop="adminName">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="adminPwd">
          <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-row>
          <el-link @click="register">注册</el-link>
        </el-row>
        <el-button
          class="loginBtu"
          type="primary"
          @click="submitForm('ruleForm')"
          >登录</el-button
        >
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { login } from "../../service/user";
import { mapActions,mapState } from "vuex";
export default {
  computed: { ...mapState("cart", ["user"]) },
  data() {
    return {
      ruleForm: {
        name: "",
        password: "",
      },
      rules: {
        name: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 3,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "change" },
          {
            min: 3,
            max: 15,
            message: "长度在 3 到 15 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    submitForm(ruleForm) {
      this.$refs[ruleForm].validate(async (valid) => {
        if (valid) {
          let response = await login(
            this.ruleForm.name,
            this.ruleForm.password
          );
          if (response.data != null) {
            
           await this.setUser();
            //验证用户权限和状态
            if (this.user.userRole == 0 && this.user.userState != 3) {
              alert("登陆成功");
              this.$router.push("/items");
              //管理员
            } else if (this.user.userRole == 2 && this.user.userState == 2) {
              alert("登陆成功");
              // console.log("manage")
              this.$router.push("/Manage");
            } else if (
              (this.user.userRole == 2 && this.user.userState == 0) ||
              this.user.userState == 1
            ) {
              alert("管理员账号未审核，不能登录");
              
            } else if (this.user.userState == 3) {
              alert("账号异常，不能登录，请联系网站管理员");
             
            }
          } else {
            alert("用户名或密码错误");
          }
        } else {
          return false;
        }
      });
    },
    //登录跳转注册
    register() {
      this.$router.push("/register");
    },
    ...mapActions("cart", ["setUser"]),
  },
};
</script>

<style scoped>
.loginPage {
  margin-top: 50px;
}

.login-card {
  width: 500px;
  text-align: center;
  margin: auto;
  height: 450px;
}

.login-form {
  padding: 60px 50px;
}

.login-form .el-form-item {
  margin-bottom: 40px;
}

.login-form .el-row {
  margin-bottom: 20px;
}

.login-form .el-link {
  float: right;
}

.loginBtu {
  width: 120px;
}
</style>
